package com.yohann.course_service.controller;


import com.yohann.course_service.entity.Video;
import com.yohann.course_service.service.VideoService;
import com.yohann.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 小节 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-10-18
 */
@Api(tags = "小节")
@RestController
@RequestMapping("/course_service/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 根据id查询小节信息
     *
     * @param id 小节id
     * @return 返回小节信息
     */
    @ApiOperation("根据id查询小节信息")
    @GetMapping("getVideoById/{id}")
    public Result getVideoById(@ApiParam("小节id") @PathVariable int id) {
        Video video = videoService.getById(id);

        return Result.ok().data("item", video);
    }

    /**
     * 添加课程小节
     *
     * @param video 小节信息
     * @return 添加状态
     */
    @ApiOperation("添加课程小节")
    @PostMapping("addVideo")
    public Result addVideo(@ApiParam("小节信息") @RequestBody Video video) {
        boolean save = videoService.save(video);

        return Result.state(save);
    }

    /**
     * 修改小节
     *
     * @param video 小节信息
     * @return 修改状态
     */
    @ApiOperation("修改小节")
    @PostMapping("updateVideo")
    public Result updateVideo(@ApiParam("小节信息") @RequestBody Video video) {
        boolean update = videoService.updateById(video);

        return Result.state(update);
    }

    /**
     * 删除小节
     *
     * @param id 小节id
     * @return 删除状态
     */
    @ApiOperation("删除小节")
    @GetMapping("deleteVideo/{id}")
    public Result deleteVideo(@ApiParam("小节id") @PathVariable int id) {
        videoService.deleteById(id);

        return Result.ok();
    }
}

